<template>
	<el-card shadow="never" header="个人信息">
		<el-form :model="form" :rules="rules" ref="form" label-width="120px" style="margin-top:20px;">
			<el-form-item label="账号">
				<el-input v-model="form.userName" disabled></el-input>
				<div class="el-form-item-msg">账号信息用于登录，系统不允许修改</div>
			</el-form-item>
			<el-form-item label="姓名">
				<el-input v-model="form.nickName"></el-input>
			</el-form-item>
			<el-form-item label="手机号码" prop="phonenumber">
				<el-input v-model="form.phonenumber" maxlength="11" />
			</el-form-item>
			<el-form-item label="性别">
				<el-select v-model="form.sex" placeholder="请选择">
					<el-option label="保密" value="2"></el-option>
					<el-option label="男" value="0"></el-option>
					<el-option label="女" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="邮箱" prop="email">
				<el-input v-model="form.email" maxlength="50" />
			</el-form-item>
			<el-form-item label="个性签名">
				<el-input v-model="form.remark" type="textarea"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="save">保存</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					nickName: "",
					phonenumber: "",
					sex: "",
					email: "",
					remark: ""
				 },
				rules: {
				nickName: [
					{ required: true, message: "用户昵称不能为空", trigger: "blur" }
				],
				email: [
					{ required: true, message: "邮箱地址不能为空", trigger: "blur" },
					{
						type: "email",
						message: "请输入正确的邮箱地址",
						trigger: ["blur", "change"]
					}
				],
				phonenumber: [
					{ required: true, message: "手机号码不能为空", trigger: "blur" },
					{
						pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
						message: "请输入正确的手机号码",
						trigger: "blur"
					}
				]
			}
			}
		},
		created(){
			this.getUserInfo();
		},
		methods: {
			getUserInfo(){
				var userInfo = this.$TOOL.data.get("USER_INFO").user;
				Object.assign(this.form, userInfo);
			},
			async save(){
				this.$refs.form.validate(async (valid) => {
					if (valid) {
						var res = await this.$API.system.user.updateUserProfile.put(this.form);
						if (res.code == 200){
							var userInfo = await this.$API.system.user.getInfo.get()
							this.$TOOL.data.set("USER_INFO", userInfo)
							this.$message.success("修改成功")
						}
					}
				});
			}
		}
	}
</script>

<style>
</style>
